var utils = require("./utils");
var constant = require("./constant");
var ad_util_is_to_continue = null;
var ad_util_to_continue = null;
var ad_util = {
  sleep_and_log(duration) {
    var interval = 2;
    for (; duration > interval; duration -= interval) {
      if (utils.exists_text_starts_with("已领取")) {
        var after_interval = random(3, 6);
        console.log(
          "ad_util sleep_and log: 找到【已领取】,再过" +
            after_interval +
            "s后停止当前广告"
        );
        sleep(after_interval * 1000);
        return;
      } else {
        console.log("ad_util sleep_and log: 没找到【已领取】");
      }
      if (utils.exists_text_starts_with("已完成任务")) {
        var after_interval = random(3, 6);
        console.log(
          "ad_util sleep_and log: 找到【已完成任务】,再过后" +
            after_interval +
            "s后停止当前广告"
        );
        sleep(after_interval * 1000);
        return;
      } else {
        console.log("ad_util sleep_and log: 没找到【已完成任务】");
      }
      sleep(interval * 1000);
      console.log("ad_util sleep_and log: 还剩:" + (duration - interval) + "s");
    }
    if (duration > 0) {
      sleep(duration * 1000);
      console.log("ad_util sleep_and log: duration ", duration, "s");
    }
  },
  run() {
    var ad_interval = constant.min_ad_time + random(1, 3);
    this.sleep_and_log(ad_interval);
    console.log("ad_util execute: end");
    this.go_back();
    this.after();
  },
  execute(is_to_continue, to_continue) {
    ad_util_is_to_continue = is_to_continue;
    ad_util_to_continue = to_continue;
    console.show();
    sleep(1 * 1000);
    this.run();
  },
  go_back() {
    var index = 0;
    while (true) {
      if (index > 3) {
        return;
      }
      back();
      sleep(constant.min_click_after_time * 1000);
      if (utils.exists_text_starts_with("看视频赚")) {
        return;
      }
      if (utils.exists_text_starts_with("看文章赚金币")) {
        return;
      }
      if (utils.exists_text_starts_with("立即收下")) {
        return;
      }
      if (utils.exists_text_starts_with("开心收下")) {
        return;
      }
      if (utils.exists_text_starts_with("残忍离开")) {
        return;
      }
      if (utils.exists_text_starts_with("换个视频")) {
        return;
      }
      if (utils.exists_text_starts_with("再看一个领取")) {
        return;
      }
      if (utils.exists_text_starts_with("再看一条领取")) {
        return;
      }
      if (utils.exists_text_starts_with("再看1个获得")) {
        return;
      }
      if (
        utils.exists_text_starts_with("百度") &&
        utils.exists_text_starts_with("视频") &&
        utils.exists_text_starts_with("小说")
      ) {
        console.log("go_back 找到【百度】【视频】【小说】");
        return;
      }
      if (utils.exists_text_starts_with("面对面")) {
        console.log("go_back 找到【面对面】");
        return;
      }
      if (utils.exists_text_starts_with("拒绝")) {
        utils.click_text("拒绝");
        console.log("go_back 找到【拒绝】");
        sleep(constant.min_click_after_time * 1000);
        back();
        sleep(constant.min_click_after_time * 1000);
      }
      index++;
      console.log("go_back index: ", index);
    }
  },
  after() {
    sleep(constant.min_ad_after_time * 1000);
    var ui_text_1 = utils.exists_text_starts_with("换个视频");
    if (ui_text_1) {
      console.log("gold_video after: 找到【换个视频】");
      utils.click_text_starts_with("换个视频");
      sleep(constant.min_click_after_time * 1000);
      this.run();
      return;
    }
    var ui_text_2 = utils.exists_text_starts_with("再看一个领取");
    if (ui_text_2) {
      console.log("ad_util after: 找到【再看一个领取】" + ui_text_2.text());
      var gold = utils.get_gold(ui_text_2.text());
      console.log("ad_util after: " + gold + "金币");
      utils.click_text_starts_with("再看一个领取");
      sleep(constant.min_click_after_time * 1000);
      this.run();
      return;
    }
    var ui_text_3 = utils.exists_text_starts_with("再看1个获得");
    if (ui_text_3) {
      console.log("ad_util after: 找到【再看1个获得】");
      utils.click_text_starts_with("再看1个获得");
      sleep(constant.min_click_after_time * 1000);
      this.run();
      return;
    }
    var ui_text_4 = utils.exists_text_starts_with("再看一条领取");
    if (ui_text_4) {
      console.log("ad_util after: 找到【再看一条领取】" + ui_text_4.text());
      var gold = utils.get_gold(ui_text_4.text());
      console.log("ad_util after: " + gold + "金币");
      utils.click_text_starts_with("再看一条领取");
      sleep(constant.min_click_after_time * 1000);
      this.run();
      return;
    }
    console.log(
      "ad_util after: 没找到【再看一个领取】或【再看1个获得】或【换个视频】或【再看一条领取】"
    );
    if (ad_util_is_to_continue && ad_util_is_to_continue()) {
      //是否有下一步
      ad_util_to_continue && ad_util_to_continue();
    } else {
      this.next();
    }
  },
  next() {
    console.hide();
    sleep(1 * 1000);
    console.log("ad_util next");
  },
};

module.exports=ad_util